import { mapTree } from 'amis/lib/utils/helper'

export const schema = {
  type: 'page',
  body: {
    type: 'lib-crud',
    api: '$preset.apis.list',
    filter: '$preset.forms.filter',
    filterTogglable: true,
    perPageField: 'size',
    pageField: 'page',
    perPageAvailable: [50, 100, 200],
    defaultParams: {
      size: 50,
    },
    headerToolbar: [
      'filter-toggler',
      {
        type: 'columns-toggler',
        align: 'left',
      },

      {
        $preset: 'actions.add',
        align: 'right',
      },
    ],
    footerToolbar: ['statistics', 'switch-per-page', 'pagination'],
    columns: [
      {
        name: 'id',
        label: 'ID',
        type: 'text',
      },
      {
        name: 'name',
        label: '模型名称',
        type: 'text',
      },

      {
        name: 'code',
        label: '模型标识',
        type: 'text',
      },

      {
        name: 'desc',
        label: '描述',
        type: 'tpl',
        tpl: '<span class="text-ellipsis" title="${remark}">${remark}</span>',
        width: 150,
      },
      {
        name: 'create_time',
        label: '创建时间',
        type: 'datetime',
      },
      {
        name: 'update_time',
        label: '更新时间',
        type: 'datetime',
      },
      {
        name: 'operator_name',
        label: '操作人',
        type: 'text',
      },
      {
        type: 'operation',
        label: '操作',
        width: 60,
        limits: ['editItem'],
        limitsLogic: 'or',
        buttons: [
          '$preset.actions.edit',
          '$preset.actions.user_app_fields',
          '$preset.actions.sync',
          '$preset.actions.del',
        ],
      },
    ],
  },
  definitions: {
    updateControls: {
      controls: [
        {
          type: 'text',
          name: 'name',
          label: '模型名称',
          required: true,
        },

        {
          type: 'text',
          name: 'code',
          label: '模型标识',
          required: true,
        },
        {
          type: 'text',
          name: 'remark',
          label: '描述',
        },
      ],
    },
  },
  preset: {
    actions: {
      add: {
        limits: 'editItem',
        type: 'button',
        align: 'right',
        actionType: 'dialog',
        label: '添加',
        icon: 'fa fa-plus pull-left',
        size: 'sm',
        primary: true,
        dialog: {
          title: '新增',
          body: {
            type: 'form',
            name: 'sample-edit-form',
            api: '$preset.apis.add',
            $ref: 'updateControls',
          },
        },
      },

      edit: {
        limits: 'editItem',
        type: 'button',
        icon: 'fa fa-pencil',
        tooltip: '编辑',
        actionType: 'dialog',
        dialog: {
          title: '编辑',
          body: {
            type: 'form',
            name: 'sample-edit-form',
            api: '$preset.apis.edit',
            $ref: 'updateControls',
          },
        },
      },
      user_app_fields: {
        limits: 'editItem',
        icon: 'fa fa-link ',
        tooltip: '模型字段',
        type: 'action',
        actionType: 'link',
        link: '/demo/model/user_app_fields?model_id__in=$id&model=$id',
      },
      del: {
        limits: 'editItem',
        type: 'button',
        icon: 'fa fa-times text-danger',
        actionType: 'ajax',
        tooltip: '删除',
        confirmText: '您确认要删除?',
        api: '$preset.apis.del',
        messages: {
          success: '删除成功',
          failed: '删除失败',
        },
      },
      sync: {
        limits: 'editItem',
        type: 'button',
        icon: 'fa fa-bullseye ',
        actionType: 'ajax',
        tooltip: '同步',
        confirmText: '您确认要同步?',
        api: '$preset.apis.sync',
        messages: {
          success: '同步成功',
          failed: '同步失败',
        },
      },
    },
    forms: {
      filter: {
        controls: [
          {
            type: 'text',
            name: 'search',
            label: '关键字',
            clearable: true,
            placeholder: '',
          },

          {
            type: 'submit',
            className: 'm-l',
            label: '搜索',
            level: 'primary',
          },
          {
            type: 'wrapper',
            className: 'no-wrapper m-l a-Form-item a-Form-item--inline ',
            body: [
              {
                type: 'action',
                actionType: 'link',
                link: '/demo/model/user_app',
                label: '重置',
              },
            ],
          },
        ],
      },
    },
  },
}

const options = {
  props: {
    locale: 'en',
  },
}

export default options
